import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ViewUI from 'view-design';
import 'view-design/dist/styles/iview.css';
import * as kit from './common/commonKit.js'
import * as permission from './common/permission.js'
import VueMeta from 'vue-meta'

Vue.use(VueMeta)


Vue.config.productionTip = false
Vue.use(ViewUI)
// 常用工具箱
Vue.prototype.$kit = kit
// 权限定义
Vue.prototype.$res = permission.permission

// 权限校验工具
Vue.prototype.$hasPermission = function (permissionId) {
    return kit.getResources(permissionId) !== undefined
}

// 根据权限编号获取具体权限地址
Vue.prototype.getPermissionUrl = function (permissionId) {
    return kit.getResources(permissionId);
}


// 获取用户信息
Vue.prototype.getUserInfo = function () {
    return JSON.parse(sessionStorage.getItem("userInfo"))
}

Vue.prototype.dateFormat = function (fmt, date) {
    let opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
    };
    let ret;
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        }
    }
    return fmt;
}


// 自定义的权限指令
Vue.directive('has', {
    bind: function (el, binding) {
        let res = kit.getResources(binding.value)
        if (res === undefined) {
            el.style.display = 'none'
            el.remove()
        }
    }
})
// 路由变化前处理,用于拦截没有登录就直接进入到了静态页面.
router.beforeEach((to, from, next) => {
    if (to.path !== '/login') {
        if (kit.getSession(kit.headerName)) {
            return next()
        } else {
            //如果没有获取到session登录凭证就直接跳转到登录页面
            return next('/login')
        }
    } else {
        return next()
    }
})

// 表格动态权限列处理
Vue.prototype.showColumn = function (column) {
    let columns = [];
    column.forEach(col => {
        if (col.has) {
            if (kit.getResources(col.has)) {
                columns.push(col);
            }
        } else {
            columns.push(col);
        }
    })
    return columns;
}

// 显示全局遮罩层
Vue.prototype.showLoading = function (value) {
    this.$Spin.show({
        render: (h) => {
            return h('div', [
                h('Icon', {
                    'class': {animation: 'ani-demo-spin 1s linear infinite;'},
                    props: {
                        type: 'ios-loading',
                        size: 18
                    }
                }),
                h('div', value)
            ])
        }
    });
}


new Vue({
    router,
    render: h => h(App)
}).$mount('#app')
